#pragma once
#include<stddef.h>
typedef struct Unit Unit;
typedef struct InPack{
	Unit* in;
	unsigned int outport;//输入的输出端口
}InPack;
typedef struct OutPack{
	size_t value;
	size_t cache;
}OutPack;
typedef struct Unit{
	unsigned int nin,nout;
	InPack* in;//模块时in是子模块且托管内存 outport是映射输入端口
	OutPack* out;//模块时对应in的输出元件 cache是输出端口
	void(*action)(Unit*);
	void(*update)(Unit*);//将action计算结果更新到输出上
	unsigned char ismodule;
}Unit;
extern Unit* newunit(unsigned int nin,unsigned int nout,void(*action)(Unit*),
void(*update)(Unit*));
/*传入共nin个子元件
前nout个皆为输入输出端口(unit*+unsigned int inport+unsigned int outport)
后nin-nout个为输入端口(unit*+unsigned int inport)
传入子元件最后开始模拟前需要连接好除输入输出外的所有端口
*/
extern Unit* newmodule(unsigned int nin,unsigned int nout,...);
extern void freeunit(Unit*);
extern void rununits(Unit** units,size_t nunit);
//自行检查端口连接是否正确
extern void connectunit(Unit* from,unsigned int outport,Unit* to,unsigned int inport);

//恒输出1
extern Unit* newalwaystrue();
//恒输出0
extern Unit* newalwaysfalse();
extern Unit* newnand1bit();
extern Unit* newand1bit();
extern Unit* newor1bit();
extern Unit* newxor1bit();
extern Unit* newnot();
extern Unit* newrslatch();